﻿Imports System.IO.File
Public Class FormFailPoint

    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
        Dim MouseX As Double = PictureBox1.PointToClient(MousePosition).X
        Dim MouseY As Double = PictureBox1.PointToClient(MousePosition).Y
        PictureBox1.Refresh()
        TextBox_X.Text = MouseX
        TextBox_Y.Text = MouseY
        PointMark(curPoint).PointX = MouseX
        PointMark(curPoint).PointY = MouseY
        WriteMark(PointMark(curPoint).PointX, PointMark(curPoint).PointY, 20, curPoint, Color.Yellow, Brushes.Yellow)
        If curPoint > 1 Then
            For i = 1 To curPoint - 1
                WriteMark(PointMark(i).PointX, PointMark(i).PointY, 20, i, Color.Green, Brushes.Green)
            Next
        End If

        curPoint += 1
d:
    End Sub

    Private Sub FormFailPoint_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            curPoint = 1
            TextBox_Prd.Text = Chanpin
            Dim file_num As Integer = FreeFile()
            Dim pointFilePath As String = DebugPath & "\Tool\" & Chanpin & "\" & Chanpin & "模板点位.txt"
            Dim OpenPointFile As Boolean = Exists(pointFilePath)
            If OpenPointFile = True Then
                FileOpen(file_num, pointFilePath, OpenMode.Random)
                Dim n As Integer = 1
                Do While Not EOF(file_num)
                    FileGet(file_num, PointMark(n))
                    n += 1
                Loop
                FileClose(file_num)
            End If
            Dim OpenPointPIC As Boolean = Exists(DebugPath & "\Tool\" & Chanpin & "\" & Chanpin & ".jpg")
            If OpenPointPIC = True Then
                PictureBox1.Image = Image.FromFile(DebugPath & "\Tool\" & Chanpin & "\" & Chanpin & ".jpg")
                'imgPhoto = PictureBox1.Image
                'imgWidth = imgPhoto.Width
                'imgHeight = imgPhoto.Height
            End If

        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub
    Private Sub WriteMark(ByVal x As Integer, ByVal y As Integer, ByVal r As Integer, ByVal NO As Integer, ByRef pencolor As Color, ByVal color As Brush)
        Try
            Dim myGraphics As Graphics = Me.PictureBox1.CreateGraphics  '声明并创建一个Graphics对象
            Dim myPen As Pen = New Pen(pencolor, 1) '声明一个画笔，并设定颜色和粗细
            myPen.DashStyle = Drawing.Drawing2D.DashStyle.Solid '线型，Solid是实线

            Dim rect As Rectangle = New Rectangle(x - r / 2, y - r / 2, r, r)
            myGraphics.DrawEllipse(myPen, rect)
            myGraphics.FillEllipse(color, rect)

            Dim mBrush As New SolidBrush(Drawing.Color.Black)
            Dim mFont As New Font("微软雅黑", 10)
            myGraphics.DrawString(NO.ToString, mFont, mBrush, x - r / 2, y - r / 2)

            myGraphics.Dispose() '释放Graphics占用的资源
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button_ReSelect.Click
        Try


            If curPoint > 1 Then
                curPoint -= 1
            End If
            PictureBox1.Refresh()
            If curPoint > 1 Then
                For i = 1 To curPoint - 1
                    WriteMark(PointMark(i).PointX, PointMark(i).PointY, 20, i, Color.Green, Brushes.Green)
                Next
                WriteMark(PointMark(curPoint - 1).PointX, PointMark(curPoint - 1).PointY, 20, (curPoint - 1), Color.Yellow, Brushes.Yellow)
            End If
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button_Save.Click
        Try

            If CurScrewPara.PointCount <> curPoint - 1 Then
                MsgBox("保存失败！选点数与设定颗数不一致！！")
                GoTo d
            End If
            Dim file_num As Integer = FreeFile()
            FileOpen(file_num, DebugPath & "\Tool\" & Chanpin & "\" & Chanpin & "模板点位.txt", OpenMode.Random)
            For i = 1 To curPoint
                FilePut(file_num, PointMark(i))
            Next
            FileClose(file_num)
d:
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub
    ''' <summary>
    ''' 显示储存的mark位置
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button_Show.Click
        Try
            PictureBox1.Refresh()
            Dim file_num As Integer = FreeFile()
            Dim pointFilePath As String = DebugPath & "\Tool\" & Chanpin & "\" & Chanpin & "模板点位.txt"
            Dim OpenPointFile As Boolean = Exists(pointFilePath)
            If OpenPointFile = True Then
                FileOpen(file_num, pointFilePath, OpenMode.Random)
                Dim n As Integer = 1
                Do While Not EOF(file_num)
                    FileGet(file_num, PointMark(n))
                    n += 1
                Loop
                FileClose(file_num)
                If CurScrewPara.PointCount <> n - 2 Then
                    MsgBox("螺丝颗数与储存点数不一致:" & CurScrewPara.PointCount)
                    showFailPoint = False
                    curPoint = 1
                Else
                    showFailPoint = True
                    curPoint = n - 1
                End If
            End If
            If curPoint > 1 Then
                For i = 1 To curPoint - 1
                    WriteMark(PointMark(i).PointX, PointMark(i).PointY, 20, i, Color.Green, Brushes.Green)
                Next
            End If
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub

    Private Sub Button_Clear_Click(sender As Object, e As EventArgs) Handles Button_Clear.Click
        PictureBox1.Refresh()
        curPoint = 1
    End Sub
End Class